package br.com.unifebe.tcc.GUI;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.com.unifebe.tcc.DAO.DAOimpl;
import br.com.unifebe.tcc.UTILS.Funcoes;
import br.com.unifebe.tcc.componente.GUI.GUIUtils;
import br.com.unifebe.tcc.componente.GUI.JCFButton;
import br.com.unifebe.tcc.componente.GUI.JMLEdit;
import br.com.unifebe.tcc.componente.GUI.JMLPasswordEdit;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

@SuppressWarnings({"serial"})
public abstract class Login extends JFrame{

	private JPanel panelPrincipal;

	private JMLEdit editLogin;
	private JMLPasswordEdit editSenha;

	private JCFButton botaoLogin;

	public Login() {
		setTitle( "Login" );
		getContentPane().setLayout( new BorderLayout() );
		getContentPane().add( getPanelPrincipal(), BorderLayout.CENTER );
		setPreferredSize( new Dimension(300, 150 ) );
		pack();
		setVisible( true );
		SwingUtilities.updateComponentTreeUI(this);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setCursor(new java.awt.Cursor(Cursor.DEFAULT_CURSOR));
		this.setLocationRelativeTo(null);
	}
	
	public abstract void executarDepoisLoginValido();

	public JMLPasswordEdit getEditSenha(){
		if(editSenha == null){
			editSenha = new JMLPasswordEdit();
		}
		return editSenha;
	}

	public JMLEdit getEditLogin(){
		if(editLogin == null){
			editLogin = new JMLEdit();
		}
		return editLogin;
	}

	public JCFButton getBotaoLogin(){
		if( botaoLogin == null ){
			botaoLogin = new JCFButton( "Logar");
			botaoLogin.addActionListener( new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					consultarLogin();

				}
			});
		}
		return botaoLogin;
	}

	public JPanel getPanelPrincipal() {
		if( panelPrincipal == null ){
			panelPrincipal = new JPanel( new FormLayout(
					GUIUtils.createArrayColSpec( 12, true ),
					GUIUtils.createArrayRowSpec( 3, true )));

			CellConstraints cc = new CellConstraints();
			int currentLine = 2;

			panelPrincipal.add(new JLabel("Login"), 		cc.xywh(  2, currentLine,  7, 2, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			panelPrincipal.add(getEditLogin(), 			cc.xywh( 10, currentLine, 7, 2, CellConstraints.FILL, CellConstraints.DEFAULT));
			currentLine += 2;

			panelPrincipal.add(new JLabel("Senha"), 	cc.xywh(  2, currentLine,  7, 2, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			panelPrincipal.add(getEditSenha(), 			cc.xywh( 10, currentLine, 7, 2, CellConstraints.FILL, CellConstraints.DEFAULT));

			panelPrincipal.add(getBotaoLogin(), 			cc.xywh( 18, currentLine, 5, 2, CellConstraints.FILL, CellConstraints.DEFAULT));

		}
		return panelPrincipal;
	}


	public void consultarLogin(){
		String login = getEditLogin().getText();
		String senha = new String( getEditSenha().getPassword() );

		Boolean sucesso = DAOimpl.getInstance().login( login, senha );
		
		if( sucesso ){
			executarDepoisLoginValido();
		}else{
			Funcoes.showMessageAlert( "Login invalido" );
		}

	}
}
